<?php
/**
 * 会员中心逻辑处理模块
 * UserModel: Administrator
 * Date: 2019/3/01/0001
 * Time: 10:41
 */
namespace app\caidongweb\model;
use think\Db;
use think\Exception;

class UserModel extends BaseModel
{
    protected $table = 'cd_user';

    /**
     * 会员首页
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $where = [];
        $sta = strtotime(input('start'));
        $end = strtotime(input('end'));
        if($sta && $end){
            $where['us_atime'] = array('between',"$sta,$end");
        }
        input('caid') ? $where['us_gid'] = input('caid') : false;
        input('key') ? $where[input('city')] = input('key') : false;
        $list = $this->alias('u')->where('us_state','neq',3)->where($where)->order('us_atime desc')->paginate(50,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 伪删除的会员
     * @return \think\Paginator
     */
    public function getDelList()
    {
        $where = [];
        $sta = strtotime(input('start'));
        $end = strtotime(input('end'));
        if($sta && $end){
            $where['us_atime'] = array('between',"$sta,$end");
        }
        input('key') ? $where[input('city')] = input('key') : false;
        $list = $this->where('us_state',3)->where($where)->order('us_atime desc')->paginate(50,false,['query'=>request()->param()]);
        return $list;
    }
    /**
     * 获取个人信息
     * @param $uid 用户编号
     * @return array|false
     */
    public function getFind($uid)
    {
        $find = $this->where('us_uid',$uid)->find();
        return $find;
    }

    /**
     * 用户编辑
     * @return array
     */
    public function getEdit(){
        $data = input('post.');
        //是否修改密码
        if(empty($data['us_pwd'])){
            unset($data['us_pwd']);
        }else{
            $data['us_pwd'] = md5($data['us_pwd']);
        }
        if(empty($data['us_pwds'])){
            unset($data['us_pwds']);
        }else{
            $data['us_pwds'] = md5($data['us_pwds']);
        }
        $res = $this->where('us_uid',$data['us_uid'])->update($data);
        if($res!==false){
            $status = $this->returnData(1,'修改成功');
        }else{
            $status = $this->returnData(-1,'修改失败');
        }
        return $status;
    }

    /**
     * 用户注册
     * @return array
     */
    public function getAdd(){
        $data = input('post.');
        $phone = $this->where('us_phone',$data['us_phone'])->find();
        $usPid = $this->where('us_phone',$data['us_pid'])->whereOr('us_uid',$data['us_pid'])->find();
        if(empty($usPid)){
            return array('code'=>-1,'msg'=>'推荐人信息不正确');
        }
        if(!empty($phone)){
            return array('code'=>-1,'msg'=>'此手机号已被使用');
        }
        $data['us_pid'] = $usPid['us_uid'];
        $data['us_uid'] = rand_number();
        $data['us_pwd'] = md5('123456');
        $str = getRandomStr();
        $find = $this->where('us_pwds',$str)->find();
        if($find){
            $status = $this->returnData(-1,'财富密钥重复，请重新点击提交！');
            return $status;
        }
        $data['us_pwds'] = $str;
        $data['us_atime'] = time();
        $data['us_img_code'] = verificationCode($data['us_phone']);
        $res = $this->insert($data);
        if($res!==false){
            $status = $this->returnData(1,'添加成功! 请保存财富密钥：'.$str);
        }else{
            $status = $this->returnData(-1,'添加失败');
        }
        return $status;
    }

    /**
     * 积分变动
     * @return array
     */
    public function getPoints()
    {
        $data = input('post.');
        $find = $this->where('us_phone',$data['phone'])->find();
        if(empty($find)){
            return array('code'=>-1,'msg'=>'账户信息不正确');
        }
        if($data['points'] < 0){
            return array('code'=>-1,'msg'=>'变动金额不正确');
        }
        if($data['type'] == 1){
            $res = accountMoneyLog($find['us_uid'],$data['points'],$data['desc']);
        }else{
            if($data['points'] > $find['us_points']){
                return array('code'=>-1,'msg'=>'账户余额不足');
            }
            $res = accountMoneyLog($find['us_uid'],-$data['points'],$data['desc']);
        }
        if($res){
            return $this->returnData(1,'资金变动成功');
        }else{
            return array('code'=>-1,'msg'=>'哎呀，网络好像跑丢了！请刷新重试');
        }
    }

    /**
     * 获取个人积分
     * @return array
     */
    public function getPonFind()
    {
        $phone = input('phone');
        $usFind = $this->where('us_phone',$phone)->find();
        if(empty($usFind)){
            return array('code'=>-1,'msg'=>'用户信息不存在');
        }
        return array('code'=>1,'msg'=>$usFind['us_points']);
    }

    /**
     * 删除用户的所有信息
     * （待完善）
     * @return array
     */
    public function getDel()
    {
        $uid = input('id');
        if($uid == '88888888'){
            return $this->returnData(-1,'此用户不可删除');
        }
        try{
            Db::startTrans();
            $artDel = db('logs')->where('lo_name',$uid)->delete();
            $artDel = db('user')->where('us_uid',$uid)->delete();
            $artDel = db('money_log')->where('ml_uid',$uid)->delete();
            Db::commit();
            return $this->returnData(1,'删除成功');
        } catch (Exception $e){
            Db::rollback();
            $this->error($e);
        }
    }
}